package br.edu.sistemaacademico.builder;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import br.edu.sistemaacademico.builder.impl.DeleteSQLBuilder;
import br.edu.sistemaacademico.builder.impl.InsertSQLBuilder;
import br.edu.sistemaacademico.builder.impl.SelectSQLBuilder;
import br.edu.sistemaacademico.builder.impl.UpdateSQLBuilder;
import br.edu.sistemaacademico.dao.DaoGenerico;
import br.edu.sistemaacademico.pojo.Historico;
import br.edu.sistemaacademico.pojo.Matricula;
import br.edu.sistemaacademico.pojo.Turma;

public class HistoricoDAO extends DaoGenerico {

	public static final List<String> COLUNAS = Arrays.asList(new String[] {
			"matricula", "turma", "media", "dataInscricao", "faltas",
			"mediaTurma", "situacao" });
	
	public List<Object> getCampos(){
		return (List) COLUNAS;
	}
	@Override
	public void alterar(Object o) throws SQLException {
		Historico h = (Historico) o;
		List<Object> valores = Arrays.asList(new Object[] { h.getMatricula().getId(),
				h.getTurma().getId(), h.getMedia(), h.getDataIncricao(), h.getFaltas(),
				h.getMediaTurma(), h.getSituacao() });

		String condicao = "matricula = " + h.getMatricula().getId();
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_HISTORICO");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao);

		dir.buildSQL(update);
		
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Object getObject(String condicao, String tabelasAdicionais) throws SQLException {
		Historico h = new Historico();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_HISTORICO a"+tabelasAdicionais);
		select.setColunas(COLUNAS);
		// select.setValores(valores);
		select.setCondicao(condicao);
		select.colunasToReturn = "a.*";

		dir.buildSQL(select);
		try {
			ResultSet rs = executeQuery(select.getSQL());
			if (rs.next())
				h = (Historico) informacoes(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return h;
	}

	@Override
	public void gravar(Object o) throws SQLException {
		Historico h = (Historico) o;
		List<Object> valores = Arrays.asList(new Object[] { h.getMatricula().getId(),
				h.getTurma().getId(), h.getMedia(), h.getDataIncricao(), h.getFaltas(),
				h.getMediaTurma(), "cursando" });

		SQLDirector dir = new SQLDirector();
		InsertSQLBuilder insert = new InsertSQLBuilder();

		insert.setNomeTabela("tb_HISTORICO");
		insert.setColunas(COLUNAS);
		insert.setValores(valores);

		dir.buildSQL(insert);
		try {
			executeCommand(insert.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public List<Object> getAll(String condicao, String tabelasAdicionais, String ordenacao){
		List<Historico> list = new LinkedList<Historico>();
		SQLDirector dir = new SQLDirector();
		SelectSQLBuilder select = new SelectSQLBuilder();

		select.setNomeTabela("tb_HISTORICO a"+tabelasAdicionais);
		select.setColunas(COLUNAS);
		// select.setValores(valores);
		select.setCondicao(condicao+ " or situacao='cursando' or situacao='aprovado' or situacao='reprovado'");
		select.colunasToReturn = "a.*";
		select.orderBy = ordenacao;
		
		dir.buildSQL(select);
		System.out.println(select.getSQL());
		try {
			ResultSet rs = executeQuery(select.getSQL());
			if (rs.next())
				list.add((Historico) informacoes(rs));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return (List) list;
	}

	@Override
	public Object informacoes(ResultSet rs) throws SQLException {
		Historico a = new Historico();
		MatriculaDAO matriculaDAO = new MatriculaDAO();
		TurmaDAO turmaDAO = new TurmaDAO();
		a.setMatricula((Matricula)matriculaDAO.getObject(" codigo="+rs.getLong(COLUNAS.get(0)), ""));
		a.setTurma((Turma)turmaDAO.getObject(" codigo="+rs.getLong(COLUNAS.get(1)), ""));
		//a.getMatricula().setId(rs.getLong(COLUNAS.get(0)));
		//a.getTurma().setId(rs.getLong(COLUNAS.get(1)));
		a.setMedia(rs.getFloat(COLUNAS.get(2)));
		a.setDataIncricao(rs.getString(COLUNAS.get(3)));
		a.setFaltas(rs.getInt(COLUNAS.get(4)));
		a.setMediaTurma(rs.getFloat(COLUNAS.get(5)));
		a.setSituacao(rs.getString(COLUNAS.get(6)));
		return a;
	}

	@Override
	public void remover(Object o) throws SQLException {
		Historico h = (Historico) o;
		List<Object> valores = Arrays.asList(new Object[] { h.getMatricula().getId(),
				h.getTurma().getId(), h.getMedia(), h.getDataIncricao(), h.getFaltas(),
				h.getMediaTurma() });

		SQLDirector dir = new SQLDirector();
		DeleteSQLBuilder delete = new DeleteSQLBuilder();

		delete.setNomeTabela("tb_Historico");
		delete.setColunas(COLUNAS);
		delete.setValores(valores);
		dir.buildSQL(delete);
		try {
			executeCommand(delete.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void deletar(Object o) throws SQLException {
		Historico h = (Historico) o;
		List<Object> valores = Arrays.asList(new Object[] { h.getMatricula().getId(),
				h.getTurma().getId(), h.getMedia(), h.getDataIncricao(), h.getFaltas(),
				h.getMediaTurma(), "cancelado" });

		String condicao = "matricula = " + h.getMatricula().getId();
		SQLDirector dir = new SQLDirector();
		UpdateSQLBuilder update = new UpdateSQLBuilder();

		update.setNomeTabela("tb_HISTORICO");
		update.setColunas(COLUNAS);
		update.setValores(valores);
		update.setCondicao(condicao +"and situacao <> 'concluido' or situacao <> 'cancelado'");

		dir.buildSQL(update);
		
		try {
			executeCommand(update.getSQL());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


}
